package com.lanwf.adminsuper.sys.login;

import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.lanwf.admin.BaseController;
import com.lanwf.admin.interceptor.AdminLockscreenInterceptor;
import com.lanwf.admin.interceptor.AdminLoginInterceptor;
import com.lanwf.admin.utils.Cons;
import com.lanwf.adminsuper.sys.utils.Cont;
import com.lanwf.adminsuper.sys.utils.GenUtil;

/**
 * 登录控制层
 */
@Path(value = "/super-admin/login", viewPath = "/superadmin/login")
public class LoginController extends BaseController {

    @Inject
    LoginService service;

    @Clear({AdminLoginInterceptor.class, AdminLockscreenInterceptor.class})
    public void login(){
        render("index.html");
    }

    /**
     * 显示登录界面
     */
    @Clear
    public void index() {
        set("prj", Cons.prj);
        render("index.html");
    }

    @Clear
    public void doLogin() {
        String username = get("username", "");
        String password = get("password", "");
         boolean b = validateCaptcha("code");
            if (!b) {
            renderJson(Ret.fail("验证码输入有误!"));
            return;
        }
        Ret ret = service.doLogin(username, password, b);
        String randomUUID = StrKit.getRandomUUID();
        GenUtil.cacheMap.put(randomUUID,System.currentTimeMillis()+"");
        setCookie(Cont.SUPER_ADMIN_COOKIE,randomUUID,Cont.SUPER_ADMIN_COOKIE_expires,true);
        renderJson(ret);
    }


    /**
     * 退出登录
     */
    @Clear
    public void logout() {
        Db.delete("delete from sys_session where id = ? ", getCookie());
        removeCookie(Cons.ADMIN_COOKIE);
        renderJson(Ret.ok());
    }


    @Clear
    public void captcha() {
        renderCaptcha();
    }

}


